# -*- coding:utf-8 -*-

r"""
mongo db model
"""

import pymongo
from framework.model import Model
from config.cfg import db_cfg

class MongoModel(pymongo.MongoClient, Model):
    r"""
    mongo base class
    """
    def __init__(self, *args, **kwargs):
        """init"""
        c = tuple()
        if args:
            c = args
        else:
            c = self.load_db()
        self.__coll, self.__database = self.return_coll()
        super(MongoModel, self).__init__(*c, **kwargs)

    def load_db(self):
        """load db"""
        for items in db_cfg:
            if 'name' in items.keys() and 'type' in items.keys():
                if items.get('type') == 'mongo':
                    return items.get('host'), items.get('port')
        return

    def set_default_database(self, name):
        """set mongodb default database"""
        self._MongoClient__default_database_name = name

    def return_coll(self):
        """return"""
        return 'db_name', 'coll_name'

    def count(self, fit=None, **kwargs):
        """ap count for self"""
        return self[self.return_coll()[0]][self.return_coll()[1]].count(filter=fit, **kwargs)

    def find(self, *args, **kwargs):
        """find"""
        return self[self.return_coll()[0]][self.return_coll()[1]].find(*args, **kwargs)

    def find_one(self, fit=None, *args, **kwargs):
        """find one"""
        return self[self.return_coll()[0]][self.return_coll()[1]].find_one(filter=fit, *args, **kwargs)

    def update(self, spec, document, upsert=False, manipulate=False,
               multi=False, check_keys=True, **kwargs):
        """update"""
        return self[self.return_coll()[0]][self.return_coll()[1]].update(spec, document, upsert=False,
                                                                         manipulate=False, multi=False, check_keys=True,
                                                                         **kwargs)